package com.yeyks.hotel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.yeyks.common.base.Arguments;
import com.yeyks.common.base.PageResult;
import com.yeyks.hotel.dal.domain.HotelVipShow;
import com.yeyks.hotel.dal.dao.HotelVipShowMapper;
import com.yeyks.hotel.param.HotelVipShowAddParam;
import com.yeyks.hotel.param.HotelVipShowListParam;
import com.yeyks.hotel.param.HotelVipShowUpdateParam;
import com.yeyks.hotel.service.HotelVipShowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 * 优享vip展示酒店服务实现类
 * </p>
 *
 * @author shuai
 * @since 2020-02-05
 */
@Service
public class HotelVipShowServiceImpl extends ServiceImpl<HotelVipShowMapper, HotelVipShow> implements HotelVipShowService {

    // 获取最新的六家展示酒店
    @Override
    public List<HotelVipShow> getShowHotelList(){
        List<HotelVipShow> list = list(
                new QueryWrapper<HotelVipShow>()
                        .eq(HotelVipShow.STATUS, 1)
                        .orderByDesc(HotelVipShow.CREATE_TIME)
                        .last("limit 6")
        );
        return list;
    }

    /**
     * 更新酒店
     *
     * @param param
     * @return
     */
    @Override
    public Boolean myUpdate(HotelVipShowUpdateParam param) {
        HotelVipShow hotelVipShow = baseMapper.selectById(param.getId());
        Arguments.beanIsNull(hotelVipShow, "该展示酒店不存在");
        if (StrUtil.isNotBlank(param.getHotelName())) {
            hotelVipShow.setHotelName(param.getHotelName());
        }
        if (StrUtil.isNotBlank(param.getRoomName())) {
            hotelVipShow.setRoomName(param.getRoomName());
        }
        if (StrUtil.isNotBlank(param.getHotelPic())) {
            hotelVipShow.setHotelPic(param.getHotelPic());
        }
        if (ObjectUtil.isNotEmpty(param.getPrice())) {
            hotelVipShow.setPrice(param.getPrice());
        }
        if (ObjectUtil.isNotEmpty(param.getSavePrice())) {
            hotelVipShow.setSavePrice(param.getSavePrice());
        }
        if (ObjectUtil.isNotEmpty(param.getStatus())) {
            hotelVipShow.setStatus(param.getStatus());
        }
        hotelVipShow.setUpdateTime(param.getUpdateTime());
        Arguments.isFalse(1 == baseMapper.updateById(hotelVipShow), "数据异常");
        return true;
    }

    /**
     * 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<HotelVipShow> myList(HotelVipShowListParam param) {
        QueryWrapper<HotelVipShow> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc(HotelVipShow.CREATE_TIME);
        if (ObjectUtil.isNotEmpty(param.getStatus())) {
            wrapper.eq(HotelVipShow.STATUS, param.getStatus());
        }
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<HotelVipShow> hotelVipShows = baseMapper.selectList(wrapper);
        return PageResult.getPageResult(hotelVipShows);
    }

    /**
     * 新增
     *
     * @param param
     * @return
     */
    @Override
    public Boolean mySave(HotelVipShowAddParam param) {
        List<HotelVipShow> hotelVipShows = baseMapper.selectList(new QueryWrapper<HotelVipShow>().eq(HotelVipShow.STATUS, 1));
        Arguments.isTrue(null != hotelVipShows && 6 <= hotelVipShows.size(), "展示酒店最多6个");
        Arguments.isFalse(1 == baseMapper.insert(BeanUtil.toBean(param, HotelVipShow.class)), "新增异常");
        return true;
    }
}
